package com.fish.yssb.ui.page.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fish.yssb.Consts;
import com.fish.yssb.R;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.data.model.TopRK;
import com.fish.yssb.ui.page.activity.MainActivity;
import com.fish.yssb.ui.page.activity.base.TmpActivity;
import com.fish.yssb.ui.page.activity.constPara.ConstParaActivity;
import com.fish.yssb.ui.page.activity.his.HisListActivity;
import com.fish.yssb.ui.page.activity.other.RestActivity;
import com.fish.yssb.ui.page.activity.realTime.RealTimeListActivity;
import com.fish.yssb.ui.page.activity.wave.CtrlActivity;
import com.fish.yssb.ui.page.activity.wave.WaveActivity;
import com.fish.yssb.ui.page.dialog.PublicNoticeDialog;
import com.fish.yssb.ui.page.dialog.ShowSQInfoDialog;
import com.fish.yssb.ui.page.fragment.MainFragment;
import com.fish.yssb.ui.page.util.TopToastUtil;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by zhuguohui on 2016/11/8.
 */

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.MyViewHolder> {

//    private static List<String> data = new ArrayList<>();

    private TimePickerView pvTime;

    private Random random=new Random();
    private Context context ;
    private ShowSQInfoDialog noticeDialog;
    private  static List<TopRK> data = new ArrayList<>();
    private  MainFragment m_mainFragment;
    public AppAdapter(Context context, List<TopRK> topAppData, MainFragment mainFragment){
        this.data = topAppData;
        this.context = context;
        m_mainFragment=mainFragment;
        initTimePicker();
    }

    public AppAdapter(Context context, List<TopRK> topAppData){
        this.data = topAppData;
        this.context = context;
//        m_mainFragment=mainFragment;
    }



    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.top_app_layout_item, parent, false);
        return new MyViewHolder(view);
    }



    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final TopRK app = data.get(position);

       final LoginUser user = BaseApp.getInstance().user;

        holder.tv_title.setText(app.getTitle());
        if(Consts.toprk1.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_1);
        }else  if(Consts.toprk2.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_2);
        }else  if(Consts.toprk3.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_3);
        }else  if(Consts.toprk4.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_4);
        }else  if(Consts.toprk5.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_5);
        }else  if(Consts.toprk6.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_6);
        }else  if(Consts.toprk7.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_7);
        }else  if(Consts.toprk8.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_8);
        }else  if(Consts.toprk9.equalsIgnoreCase(app.getTitle()))
        {

            if("".equalsIgnoreCase(user.getUserName()))
            {
                holder.im_icon.setImageResource(R.drawable.top_rk_9_u);
            }else{
                holder.im_icon.setImageResource(R.drawable.top_rk_9);
            }

        }else  if(Consts.toprk10.equalsIgnoreCase(app.getTitle()))
        {
            if("".equalsIgnoreCase(user.getUserName()))
            {
                holder.im_icon.setImageResource(R.drawable.top_rk_9_u);
            }else{
                holder.im_icon.setImageResource(R.drawable.top_rk_9);
            }
        }else  if(Consts.toprk11.equalsIgnoreCase(app.getTitle()))
        {
            if("".equalsIgnoreCase(user.getUserName()))
            {
                holder.im_icon.setImageResource(R.drawable.top_rk_11_u);
            }else {
                holder.im_icon.setImageResource(R.drawable.top_rk_11);
            }
        }else  if(Consts.toprk12.equalsIgnoreCase(app.getTitle()))
        {
            holder.im_icon.setImageResource(R.drawable.top_rk_12);
//            if("".equalsIgnoreCase(user.getUserName()))
//            {
//                holder.im_icon.setImageResource(R.drawable.top_rk_11_u);
//            }else {
//                holder.im_icon.setImageResource(R.drawable.top_rk_11);
//            }
        }


        holder.rl_item.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(Consts.toprk1.equalsIgnoreCase(app.getTitle()))
                {
                    m_mainFragment.readSQInfo(v);


                }else  if(Consts.toprk2.equalsIgnoreCase(app.getTitle()))
                {

                    RealTimeListActivity.start(context);
                }else  if(Consts.toprk3.equalsIgnoreCase(app.getTitle()))
                {
                    ConstParaActivity.start(context);
                }else  if(Consts.toprk4.equalsIgnoreCase(app.getTitle()))
                {
                    HisListActivity.start(context);
                }else  if(Consts.toprk5.equalsIgnoreCase(app.getTitle()))
                {
                    WaveActivity.start(context);
                }else  if(Consts.toprk6.equalsIgnoreCase(app.getTitle()))
                {
//                    pvTime.show(v);
                    m_mainFragment.syncTime( v);

                }else  if(Consts.toprk7.equalsIgnoreCase(app.getTitle()))
                {

                    CtrlActivity.start(context);
                }else  if(Consts.toprk8.equalsIgnoreCase(app.getTitle()))
                {

                    m_mainFragment.show();
                }else  if(Consts.toprk9.equalsIgnoreCase(app.getTitle()))
                {

                    if(!"".equalsIgnoreCase(user.getUserName()))
                    {
                        RestActivity.start(context);

                    }

                }else  if(Consts.toprk10.equalsIgnoreCase(app.getTitle()))
                {
                    TmpActivity.start(context);
                    if(!"".equalsIgnoreCase(user.getUserName()))
                    {

                    }
                }else  if(Consts.toprk11.equalsIgnoreCase(app.getTitle()))
                {
                    TmpActivity.start(context);
                    if(!"".equalsIgnoreCase(user.getUserName()))
                    {

                    }
                }else{
                    TmpActivity.start(context);
                }
            }
        });

    }



    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        View view_item;
        ImageView im_icon;
        RelativeLayout rl_item;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            im_icon = (ImageView)itemView.findViewById(R.id.im_icon);
            view_item = (View)itemView.findViewById(R.id.view_item);

            rl_item = (RelativeLayout)itemView.findViewById(R.id.rl_item);
        }
    }

    public void updateData(ArrayList<TopRK> topAppData) {


        this.data.addAll(topAppData);
        notifyDataSetChanged();

    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出
        pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
//                Toast.makeText(context, getTime(date), Toast.LENGTH_SHORT).show();
                Log.i("pvTime", "onTimeSelect");
                TopToastUtil.getInstance((Activity) context,R.layout.showdialog).showShortToast("时间同步成功");

            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");

                    }
                })
                .setTitleText("查询设置")
                .setType(new boolean[]{true, true, true, true, true, true})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

}
